home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Today - The Disc! 5 / CD-ROM Today - The Disc (Issue 5)(November 1994).ISO / mac / Mac shareware / Education / RLaB / rlib / triu.r < prev    next >
Text File  |  1994-09-21  |  839b  |  43 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    triu ( A )
  4. //        triu ( A , K )
  5.  
  6. //  Description:
  7.  
  8. //  triu(x) returns the upper triangular part of A.
  9.  
  10. //  tril(x; k) returns the elements on and above the k-th diagonal of
  11. //  A. 
  12.  
  13. //  K = 0: main diagonal
  14. //  K > 0: above the main diag.
  15. //  K < 0: below the main diag.
  16.  
  17. //  See Also: tril
  18. //-------------------------------------------------------------------//
  19.  
  20. triu = function(x, k) 
  21. {
  22.   local(i, j, nr, nc, y);
  23.  
  24.   if (!exist (k)) { k = 0; }
  25.   nr = x.nr; nc = x.nc;
  26.  
  27.   if(k > 0) 
  28.   { 
  29.     if (k > (nc - 1)) { error ("triu: invalid value for k"); }
  30.   else
  31.     if (abs (k) > (nr - 1)) { error ("triu: invalid value for k"); }
  32.   }
  33.  
  34.   y = zeros(nr, nc);
  35.  
  36.   for(j in max( [1,1+k] ):nc) {
  37.     i = 1:min( [nr, j-k] );
  38.     y[i;j] = x[i;j];
  39.   }
  40.  
  41.   return y;
  42. };
  43.